/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.ui;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;

import org.kommando.core.Displayable;
import org.kommando.core.search.Result;
import org.kommando.core.search.ScoredResult;

/**
 * {@link ListCellRenderer} implementation for displaying {@link Displayable}s.
 * 
 * @author Peter De Bruycker
 */
public class ResultListCellRenderer extends DefaultListCellRenderer {

    // TODO this class contains code very similary to the ResultListCellRenderer

    private static final long serialVersionUID = 1L;

    private Dimension iconSize;

    public ResultListCellRenderer(Dimension iconSize) {
        this.iconSize = iconSize;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        Result<?> result = (Result<?>) value;

        float alpha = 1;

        if (list.getModel().getSize() > 0) {

            if (result instanceof ScoredResult<?>) {
                // determine the "fade" factor
                ScoredResult<?> scoredResult = (ScoredResult<?>) result;

                float firstScore = getFirstScore(list.getModel());
                float score = scoredResult.getScore();

                alpha = (float) score / (float) firstScore;
                if (alpha < 1) {
                    alpha = alpha * 0.7f;
                }
                alpha = Math.max(alpha, 0.4f);
                alpha = Math.min(alpha, 1.0f);
            }
        }

        setText(result.getHtmlFormattedName() + "<p>" + result.getDescription());
        setIcon(result.getIcon(iconSize));
        setToolTipText(result.getToolTip());

        return new AlphaCompositeDecorator(this, alpha);
    }

    private float getFirstScore(ListModel listModel) {
        for (int i = 0; i < listModel.getSize(); i++) {
            Result<?> element = (Result<?>) listModel.getElementAt(i);
            if (element instanceof ScoredResult<?>) {
                return ((ScoredResult<?>) element).getScore();
            }
        }

        return 0;
    }
}
